package com.csun.cmny.provider.service.impl;

import com.csun.cmny.base.enums.ErrorCodeEnum;
import com.csun.cmny.core.genertor.UniqueIdGenerator;
import com.csun.cmny.core.support.BaseService;
import com.csun.cmny.provider.dao.UacUserSecurityMapper;
import com.csun.cmny.provider.dao.UacUserSecurityQuestionMapper;
import com.csun.cmny.provider.model.constant.SecurityConstant;
import com.csun.cmny.provider.model.domain.UacUser;
import com.csun.cmny.provider.model.domain.UacUserSecurity;
import com.csun.cmny.provider.model.domain.UacUserSecurityQuestion;
import com.csun.cmny.provider.model.dto.user.CheckSecurityQuestionDto;
import com.csun.cmny.provider.model.dto.user.ResetLoginPwdDto;
import com.csun.cmny.provider.model.dto.user.UserSecurityQuestionDto;
import com.csun.cmny.provider.model.exceptions.UacBizException;
import com.csun.cmny.provider.model.vo.UserSecurityVo;
import com.csun.cmny.provider.resolver.CmnyMessageSource;
import com.csun.cmny.provider.service.UacUserSecurityService;
import com.csun.cmny.provider.service.UacUserService;
import com.csun.cmny.security.core.validate.code.CookieUtil;
import com.csun.cmny.util.PubUtils;
import com.csun.cmny.util.PublicUtil;
import com.csun.cmny.util.RedisKeyUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.concurrent.TimeUnit;

@Slf4j
@Service
@Transactional
public class UacUserSecurityServiceImpl extends BaseService<UacUserSecurity> implements UacUserSecurityService {

    @Resource
    private UacUserSecurityMapper uacUserSecurityMapper;
    @Resource
    private UacUserSecurityQuestionMapper uacUserSecurityQuestionMapper;
    @Resource
    private UacUserService uacUserService;
    @Resource
    private RedisTemplate<String, Object> redisTemplate;
    @Resource
    private CmnyMessageSource cmnyMessageSource;

    @Override
    public List<UacUserSecurityQuestion> querySecurityQuestionList() {
        return uacUserSecurityQuestionMapper.querySecurityQuestionList();
    }

    @Override
    public UserSecurityVo queryUserSecurityQuestionList(String loginName) {
        UacUser uacUser = new UacUser();
        uacUser.setLoginName(loginName);
        UacUser uacUser1 = uacUserService.selectOne(uacUser);
        if(PublicUtil.isEmpty(uacUser1)){
            throw new UacBizException(ErrorCodeEnum.UAC10011002, loginName);
        }

        UacUserSecurity uacUserSecurity = new UacUserSecurity();
        uacUserSecurity.setUserId(uacUser1.getId().intValue());
        UacUserSecurity uacUserSecurity1 = uacUserSecurityMapper.selectOne(uacUserSecurity);

        UserSecurityVo userSecurityVo = new UserSecurityVo();
        if(PublicUtil.isNotEmpty(uacUserSecurity1)){
            BeanUtils.copyProperties(uacUserSecurity1,userSecurityVo);
        }

        return userSecurityVo;
    }

    @Override
    public Integer addSecurityQuestion(UserSecurityQuestionDto userSecurityQuestionDto) {

        UacUserSecurity uacUserSecurity = new UacUserSecurity();
        BeanUtils.copyProperties(userSecurityQuestionDto,uacUserSecurity);
        return uacUserSecurityMapper.insertSelective(uacUserSecurity);
    }

    @Override
    public String checkQuestionActive(CheckSecurityQuestionDto checkSecurityQuestionDto, HttpServletResponse response) {

        UacUser uacUser = new UacUser();
        uacUser.setLoginName(checkSecurityQuestionDto.getLoginName());
        UacUser uacUser1 = uacUserService.selectOne(uacUser);
        if(PublicUtil.isEmpty(uacUser1)){
            return cmnyMessageSource.getMessage("10011011",null);
//            throw new UacBizException(ErrorCodeEnum.UAC10011002, checkSecurityQuestionDto.getLoginName());
        }

        UacUserSecurity uacUserSecurity = uacUserSecurityMapper.selectByPrimaryKey(uacUser1.getId().intValue());

        if(uacUserSecurity.getCheckTimes()>=SecurityConstant.SECURITY_CHECKTIMES_MAXIMUM){
//            throw new UacBizException(ErrorCodeEnum.UAC10011051,uacUserSecurity.getCheckTimes());
            return cmnyMessageSource.getMessage("10019004",null);
        }

        if(uacUserSecurity.getQuestion1Answer().equals(checkSecurityQuestionDto.getQuestion1Answer())&&uacUserSecurity.getQuestion2Answer().equals(checkSecurityQuestionDto.getQuestion2Answer())){
            uacUserSecurity.setCheckTimes(SecurityConstant.SECURITY_CHECKTIMES_INITIAL_VALUE);
            int result = uacUserSecurityMapper.updateByPrimaryKeySelective(uacUserSecurity);

            //验证成功返回加密字符串
            String resetPwdKey = PubUtils.uuid() + UniqueIdGenerator.generateId();
            redisTemplate.opsForValue().set(RedisKeyUtil.getResetPwdTokenKey(resetPwdKey), uacUser1, 10, TimeUnit.MINUTES);
            CookieUtil.setCookie("PASSCLOUD_PAAS_resetPwdKey", resetPwdKey, 10 * 60, response);
            return resetPwdKey;
        }

        uacUserSecurity.setCheckTimes(uacUserSecurity.getCheckTimes()+1);
        int i = uacUserSecurityMapper.updateByPrimaryKeySelective(uacUserSecurity);
        return null;
    }

}
